﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using EPiServer.Core;

namespace TemplateFoundation.WebControls.PropertyControls
{
    /// <summary>
    /// Property control for PropertyLongString properties
    /// </summary>
    public class PropertyLongStringControl : EPiServer.Web.PropertyControls.PropertyLongStringControl
    {
        /// <summary>
        /// Gets or sets whether the container element should be rendered
        /// </summary>
        public bool RenderContainer
        {
            get;
            set;
        }

        /// <summary>
        /// Renders the control to the specified HTML writer.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the control content.</param>
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (RenderContainer || RenderType!=RenderType.Default)
            {
                base.Render(writer);
            }
            else
            {
                // Render string without any container element

                if (Controls != null && Controls.Count>0)
                {
                    var rootControl = Controls[0];

                    if (rootControl.Controls != null && rootControl.Controls.Count > 0)
                    {
                        writer.Write(((Literal)rootControl.Controls[0]).Text);
                    }
                }
            }
        }
    }
}
